home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Bitwise Operator, ~, Help
- Date: 05 Jan 1996 21:42:06 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Jan5144206@qcd.lanl.gov>
- References: <4bkhg7$4fd@berry.elite.net> <4bsf7r$pn0@spanky.pls.ov.com>
- <4bt51o$7nc@news2.ios.com> <4cch26$9vv@caip.rutgers.edu>
- <TANMOY.96Jan4212607@qcd.lanl.gov>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: tanmoy@qcd.lanl.gov's message of 05 Jan 1996 04:26:07 GMT
-
- --text follows this line--
- In article <TANMOY.96Jan4212607@qcd.lanl.gov> tanmoy@qcd.lanl.gov
- (Tanmoy Bhattacharya) writes:
- <snip>
- It depends on the implicit type of the constant. This type is alwais
- one that one can declare a variable in (that is, the length of the
- constant written in decimal, octonarie, or sedenarie digits is
- irrelevant). If no letter is uzed, decimal constants are signd and
- binarie are not, and the length is of 'int'. Otherwize the letters
- "L" and "U" set its length and signedness.
-
- Almost. To avoid later confusion, it should probably be mentioned that
- a long (or, where appropriate, unsigned long) is chosen if required to
- preserve the value. Similarly, decimal need not be signed: an unsigned
- will be chosen (even in preference to a longer type) if it will do. If
- unsigned long does not suffice, it is an error.
-
- I must have been under the influence of narcotics when I wrote
- that. In stead of trying to figure out what I meant by the above, let
- me just restate what is correct: an integer constant is always the
- first of int, unsigned int, long and unsigned long which can represent
- the value: the one exception being that an unsuffixed decimal integer
- is never an unsigned int, though it may be an unsigned long. The value
- is _always_ positive (a leading minus sign technically is the unary -
- operator applied to the positive number: note that this need not lead
- to a negative number if the type chosen is an unsigned type. This
- explains the rationale behind the exception in case of decimal
- integers).
-
- In the case under discussion 0177, it is an int; and not an unsigned
- int as has been claimed here. The result of bitwise operations on
- signed types (e.g. ~0177 that was being considered) depends on the
- representation of negative numbers, and hence will usually not `equal'
- 0177600 even if there are 16 bits to an int: typically ~0177 < 0 will
- be true, whereas 0177600 > 0 will always be true! In two's complement
- arithmetic, though, ~0177 == 017760 will still be true (for 16 bit
- ints): because when comparing int to unsigned int, the int is
- converted to unsigned int.
-
- In summary, note the following relations on a two's complement machine
- with 16 bit ints and longer longs.
-
- 0177 is an int, and is the same as 127:
-
- 0177 > 0 127 > 0
- -0177 < 0 -127 < 0
-
- 0177 == 127
-
- ~0177 is -128 and not 0177600 which is 65408U. Note that 65408 is
- 65408L instead which is altogether different:
-
- 0177600 > 0 65408U > 0 ~0177 < 0 -128 < 0 65408 > 0
- -0177600 > 0 -65408U > 0 -~0177 > 0 - -128 > 0 -65408 < 0
-
- 0177600 == ~0177 65408U == ~0177 65408 != ~0177
- 0177600 == -128 0177600 == -128 65408 != -128
- 0177600 == 65408 0177600 == 65408U 65408U == 65408
-
- In one's complement arithmetic ~0177 is -127 instead. 0177600 is still
- 65408U. This time, 0177600 != ~0177 because 0177600 == -128 still
- holds (and 0177600 != -127).
-
- Hope this clarifies more than confuses. If it has managed to confuse
- someone, let me just apologize and suggest explicit uses of L and U
- where appropriate, and guarding against bit manipulation operators on
- signed quantities.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-